package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbParaMapper;
import com.guigu.qingcheng.mapper.TbTemplateMapper;
import com.guigu.qingcheng.pojo.TbPara;
import com.guigu.qingcheng.pojo.TbTemplate;
import com.guigu.qingcheng.pojo.vo.ParaVo;
import com.guigu.qingcheng.service.TbParaService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbParaServiceImpl implements TbParaService {
    @Autowired(required = false)
    private TbParaMapper tbParaMapper;

    @Autowired(required = false)
    private TbTemplateMapper tbTemplateMapper;


    @Override
    public List<TbPara> queryAll() {
        List<TbPara> tbPara = tbParaMapper.selectAll();
        return tbPara;
    }

    @Override
    public PageUtil<TbPara> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<TbPara> pages = (Page<TbPara>) tbParaMapper.selectAll();
        PageUtil<TbPara> pageUtil = new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbPara> pageQueryByAll(Integer pageNum, Integer pageSize, TbPara tbPara) {

        Example example = new Example(TbPara.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if (!StringUtils.isEmpty(tbPara.getId())) {
            criteria.andEqualTo("id", tbPara.getId());
        }
        if (!StringUtils.isEmpty(tbPara.getName())) {
            criteria.andLike("name", tbPara.getName());
        }
        criteria.andEqualTo("templateId", tbPara.getTemplateId());
        PageHelper.startPage(pageNum, pageSize);
        Page<TbPara> pages = (Page<TbPara>) tbParaMapper.selectByExample(example);
        PageUtil<TbPara> pageUtil = new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
        }


        @Transactional
        @Override
        public boolean saveTbPara (TbPara tbPara){
            TbTemplate tbTemplate = tbTemplateMapper.selectByPrimaryKey(tbPara.getTemplateId());
            tbTemplate.setParaNum(tbTemplate.getParaNum()+1);
            tbTemplateMapper.updateByPrimaryKeySelective(tbTemplate);
            int insert = tbParaMapper.insert(tbPara);
            if (insert > 0) {
                return true;
            }
            return false;
        }

        @Override
        public boolean updateTbPara (TbPara tbPara){
            int i = tbParaMapper.updateByPrimaryKeySelective(tbPara);
            if (i > 0) {
                return true;
            }
            return false;
        }


        @Transactional
        @Override
        public boolean deleteById (Integer id){
            TbPara tbPara = tbParaMapper.selectByPrimaryKey(id);
            TbTemplate tbTemplate = tbTemplateMapper.selectByPrimaryKey(tbPara.getTemplateId());
            tbTemplate.setParaNum(tbTemplate.getParaNum()-1);
            tbTemplateMapper.updateByPrimaryKeySelective(tbTemplate);
            int i = tbParaMapper.deleteByPrimaryKey(id);
            if (i > 0) {
                return true;
            }
            return false;
        }

        @Override
        public TbPara findById (Integer id){
            TbPara tbPara = tbParaMapper.selectByPrimaryKey(id);
            return tbPara;
        }

    @Override
    public PageUtil<ParaVo> ParaPageQueryByAll(Integer pageNum, Integer pageSize, TbPara tbPara) {
        List<ParaVo> paraVos = tbParaMapper.ParaPageQueryByAll(pageNum-1, pageSize, tbPara);
        Long total = tbParaMapper.ParaPageQueryByAlltotal(tbPara);
        PageUtil<ParaVo> pageUtil = new PageUtil<>();
        pageUtil.setRows(paraVos);
        pageUtil.setTotal(total);
        return pageUtil;
    }

    @Override
    public List<TbPara> selectParaByTemplateId(Integer id) {
        Example example = new Example(TbPara.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("templateId", id);
        List<TbPara> tbParas = tbParaMapper.selectByExample(example);
        return tbParas;
    }
}
